package com.langxin.site.biz;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.langxin.site.dao.ProductDao;
import com.langxin.site.model.ProductInfo;
import com.langxin.site.model.ProductTypeInfo;
import com.langxin.site.model.vo.QueryVo;
import com.langxin.site.util.constant.SiteConstant;

/**
 * 旅游biz
 * @author guqj
 *
 */
@Service
public class ProductBiz extends BaseBiz{

	@Autowired
	private ProductDao  productDao;
	
	/**
	 * 条件查询
	 * @param baseVo   参数
	 * @param pageSize 每页记录数
	 * @return
	 */
	public List<ProductInfo> queryList(QueryVo queryVo, Integer pageSize){
		if(queryVo == null){
			queryVo = new QueryVo();
		}
		if(pageSize != null){
			queryVo.setPageSize(pageSize);
		}
		return productDao.query(queryVo);
	}
	
	/**
	 * 产品分组列表
	 * @return
	 */
	public List<ProductTypeInfo> queryGroupList(){
		QueryVo queryVo = new QueryVo();
		queryVo.setPage(null);
		List<ProductInfo> list = queryList(queryVo, null);
		Map<Integer, ProductTypeInfo> map = new LinkedHashMap<>();
		ProductTypeInfo pt = null;
		List<ProductInfo> pros = null;
		for(ProductInfo p : list){
			pt = map.get(p.getTypeId());
			if(pt == null){
				pt = new ProductTypeInfo();
				pt.setName(p.getType());
				pros = new ArrayList<ProductInfo>();
				pros.add(p);
				pt.setProds(pros);
				map.put(p.getTypeId(), pt);
			}else{
				pt.getProds().add(p);
			}
		}
		
		List<ProductTypeInfo> dataList = new ArrayList<ProductTypeInfo>();
	    for(Map.Entry<Integer, ProductTypeInfo> result: map.entrySet()){
	    	dataList.add(result.getValue());
	    }
		return dataList;
	}
	
	/**
	 * 精品推荐
	 * @return
	 */
	public List<ProductInfo> queryGifts(Integer num){
		QueryVo queryVo = new QueryVo();
		queryVo.setIsGifts(1);
		return queryList(queryVo, num);
	}
	
	/**
	 * 详情
	 * @param id
	 * @return
	 */
	public ProductInfo queryById(Integer id){
		if(!isRead(SiteConstant.CookieKey.PROD.getValue(), id)){
			productDao.updateReadTime(id);
		}
		return productDao.queryById(id);
	}
}
